"use strict";

/**
 * Reference to the GoogleMaps element.
 * @type {Map}
 */
var map;

/**
 * stores the map zoom level on start of own pinch zoom gesture.
 * @type {Number]
 */
var pinchStartZoomlevel;

/**
 * stares the state, if pinch zoom is currently in action.
 * @type {Boolean}
 */
var pinchZoomInAction = false;

/**
 * initialize the GoogleMaps view.
 */
function initialize(){
    var latlng = new google.maps.LatLng(startPos[0], startPos[1]);
    var myOptions = {
        mapTypeControl: false,
        overviewMapControl: false,
        panControl: false,
        panControlOptions: {
            position: google.maps.ControlPosition.RIGHT_BOTTOM
        },
        rotateControl: false,
        scaleControl: true,
        scaleControlOptions: {
            position: google.maps.ControlPosition.BOTTOM_LEFT
        },
        streetViewControl: false,
        zoomControl: true,
        zoomControlOptions: {
            position: google.maps.ControlPosition.LEFT_BOTTOM
        },
        zoom: startPos[2],
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    loadMarkers();
    if (typeof(window.bbbapp) == "undefined") {
        alert("Warning:\n\nThis web application was build for the App 'BremenByBike' that is\navailable on Goolge Play and not for a normal web browser!");
    }
    else {
        window.bbbapp.loadingFinished();
    }
}

/**
 * run this, after loading all related files.
 */
window.onload = function(){
    initialize();
}

/**
 * object for a blue map marker.
 * @type {MarkerImage}
 */
var pinImageBlue;

/**
 * object for a green map marker.
 * @type {MarkerImage}
 */
var pinImageGreen;

/**
 * object for a red map marker.
 * @type {MarkerImage}
 */
var pinImageRed;

/**
 * object for a white map marker.
 * @type {MarkerImage}
 */
var pinImageWhite;

/**
 * object for the shadow of any colered map marker.
 * @type {MarkerImage}
 */
var pinShadow;

function loadMarkers(){
	var pinSize = new google.maps.Size(21, 34);
	var pinShadowSize = new google.maps.Size(40, 37);
	var pointZero = new google.maps.Point(0, 0);
	var pinMapPos = new google.maps.Point(10, 34);
	var pinShadowMapPos = new google.maps.Point(12, 35);
    pinImageBlue = new google.maps.MarkerImage("symbol/googlemaps_marker_blue.png", pinSize, pointZero, pinMapPos);
    pinImageGreen = new google.maps.MarkerImage("symbol/googlemaps_marker_green.png", pinSize, pointZero, pinMapPos);
    pinImageRed = new google.maps.MarkerImage("symbol/googlemaps_marker_red.png", pinSize, pointZero, pinMapPos);
    pinImageWhite = new google.maps.MarkerImage("symbol/googlemaps_marker_white.png", pinSize, pointZero, pinMapPos);
    pinShadow = new google.maps.MarkerImage("symbol/googlemaps_markershadow.png", pinShadowSize, pointZero, pinShadowMapPos);
}

/**
 * move the map view point a bit left side.
 */
function moveViewLeft(){
    map.panBy(-20, 0);
}

/**
 * move the map view point a bit right side.
 */
function moveViewRight(){
    map.panBy(20, 0);
}

/**
 * move the map view point a bit top side.
 */
function moveViewTop(){
    map.panBy(0, -20);
}

/**
 * move the map view point a bit bottom side.
 */
function moveViewBottom(){
    map.panBy(0, 20);
}

/**
 * zoom one step into the map.
 */
function zoomIn(){
    map.setZoom(map.getZoom() + 1);
}

/**
 * zoom one step out from the map.
 */
function zoomOut(){
    map.setZoom(map.getZoom() - 1);
}

/**
 * start own pinch zoom gesture.
 */
function startPinchZoom(){
    pinchStartZoomlevel = map.getZoom();
    pinchZoomInAction = true;
}

/**
 * stop own pinch zoom gesture.
 */
function stopPinchZoom(){
    pinchZoomInAction = false;
}

/**
 * update zoom level during own pinch zoom gesture.
 * @param {Number} scale scale factor
 */
function updatePinchZoom(scale){
    if (pinchZoomInAction) {
        var newZoomLevel = pinchStartZoomlevel;
        if (scale > 1) {
            // zoom in.
            if (scale > 1.6) {
                newZoomLevel++;
            }
            if (scale > 3.1) {
                newZoomLevel++;
            }
        }
        else {
            // zoom out.
            if (scale < 0.7) {
                newZoomLevel--;
            }
            if (scale < 0.35) {
                newZoomLevel--;
            }
        }
        map.setZoom(newZoomLevel);
    }
}

function showRoute(points) {
	var line = new google.maps.MVCArray();
	for(var i=0; i<points.length; i+=2) {
		if(points[i] == -1) {
			continue;
		}
		var p = gk2wgs(points[i], points[i+1]);
		line.push(new google.maps.LatLng(p[0], p[1]));
	}
	var route = new google.maps.Polyline({
		clickable: false,
		editable: false,
		geodesic: false,
		map: map,
		path: line
	});
    var startMarker = new google.maps.Marker({
		clickable: false,
		position: line.getAt(0),
		map: map,
		title: name,
		icon: pinImageBlue,
		shadow: pinShadow
	});
    var endMarker = new google.maps.Marker({
		clickable: false,
		position: line.getAt(line.getLength()-1),
		map: map,
		title: name,
		icon: pinImageBlue,
		shadow: pinShadow
	});
}
